{% extends 'base.html' %}

{% block title %}{{ profile_user.username }} 的个人资料 - 个人博客{% endblock %}

{% block content %}
<div class="container py-4">
    <div class="row">
        <!-- 用户资料卡片 -->
        <div class="col-lg-4 mb-4">
            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white">
                    <h2 class="h4 mb-0">用户资料</h2>
                </div>
                <div class="card-body text-center">
                    {% if profile_user.avatar %}
                    <img src="{{ profile_user.avatar.url }}" alt="{{ profile_user.username }}" class="rounded-circle img-fluid mb-3" style="max-width: 150px;">
                    {% else %}
                    <img src="https://via.placeholder.com/150" alt="{{ profile_user.username }}" class="rounded-circle img-fluid mb-3">
                    {% endif %}
                    
                    <h3>{{ profile_user.username }}</h3>
                    
                    {% if profile_user.email %}
                    <p class="text-muted">{{ profile_user.email }}</p>
                    {% endif %}
                    
                    {% if profile_user.bio %}
                    <div class="mt-3">
                        <h5>个人简介</h5>
                        <p>{{ profile_user.bio }}</p>
                    </div>
                    {% endif %}
                    
                    {% if profile_user.website %}
                    <div class="mt-3">
                        <a href="{{ profile_user.website }}" target="_blank" class="btn btn-outline-primary">
                            <i class="fas fa-globe me-1"></i> 个人网站
                        </a>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
        
        <!-- 用户文章列表 -->
        <div class="col-lg-8">
            <div class="card shadow-sm">
                <div class="card-header bg-light">
                    <h2 class="h4 mb-0">{{ profile_user.username }} 的文章</h2>
                </div>
                <div class="card-body">
                    {% for post in user_posts %}
                    <div class="card mb-3">
                        <div class="row g-0">
                            {% if post.featured_image %}
                            <div class="col-md-3">
                                <img src="{{ post.featured_image.url }}" class="img-fluid rounded-start h-100" style="object-fit: cover;" alt="{{ post.title }}">
                            </div>
                            <div class="col-md-9">
                            {% else %}
                            <div class="col-12">
                            {% endif %}
                                <div class="card-body">
                                    <h5 class="card-title">
                                        <a href="{{ post.get_absolute_url }}" class="text-decoration-none">{{ post.title }}</a>
                                    </h5>
                                    <p class="card-text text-muted">
                                        <small>
                                            <i class="fas fa-calendar me-1"></i> {{ post.publish_date|date:"Y-m-d" }} |
                                            <i class="fas fa-eye me-1"></i> {{ post.views }} 次浏览 |
                                            <i class="fas fa-comments me-1"></i> {{ post.comments.count }} 条评论
                                        </small>
                                    </p>
                                    <p class="card-text">{{ post.summary|truncatewords:20 }}</p>
                                    <div>
                                        {% if post.category %}
                                        <a href="{% url 'category_detail' post.category.slug %}" class="badge bg-primary text-decoration-none">{{ post.category.name }}</a>
                                        {% endif %}
                                        {% for tag in post.tags.all %}
                                        <a href="{% url 'tag_detail' tag.slug %}" class="badge bg-secondary text-decoration-none">{{ tag.name }}</a>
                                        {% endfor %}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% empty %}
                    <div class="alert alert-info">
                        该用户还没有发布任何文章。
                    </div>
                    {% endfor %}
                    
                    <!-- 分页 -->
                    {% if user_posts.paginator.num_pages > 1 %}
                    <nav aria-label="Page navigation" class="mt-4">
                        <ul class="pagination justify-content-center">
                            {% if user_posts.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?page=1" aria-label="First">
                                    <span aria-hidden="true">&laquo;&laquo;</span>
                                </a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="?page={{ user_posts.previous_page_number }}" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                            {% endif %}

                            {% for num in user_posts.paginator.page_range %}
                            {% if user_posts.number == num %}
                            <li class="page-item active"><a class="page-link" href="?page={{ num }}">{{ num }}</a></li>
                            {% elif num > user_posts.number|add:'-3' and num < user_posts.number|add:'3' %}
                            <li class="page-item"><a class="page-link" href="?page={{ num }}">{{ num }}</a></li>
                            {% endif %}
                            {% endfor %}

                            {% if user_posts.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ user_posts.next_page_number }}" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="?page={{ user_posts.paginator.num_pages }}" aria-label="Last">
                                    <span aria-hidden="true">&raquo;&raquo;</span>
                                </a>
                            </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
</div>
